Page({
  data: {
    list: [],
    loading: false,
    noMore: false,
    pageIndex: 1,
    scrollTop: 0
  },
  onLoad: function() {
    this.setData({
      statusCode: require('../../config/statusCode').order
    });
  },
  onShow: async function () {
    this.setData({
      isLogined: await wx.user.hasLogin()
    }, () => {
      this.getOrders();
    });
  },
  onPullDownRefresh: function () {
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 300);
  },
  onReachBottom: function () {
    if (this.data.noMore) return;
    this.setData({
      loading: true
    });
    this.getOrders();
  },
  onPageScroll: function (e) {
    this.setData({
      scrollTop: e.scrollTop
    });
  },
  getOrders: function () {
    let d = this.data,
        list = d.list;
    wx.util.request('order.my', {
      body: {
        page: d.pageIndex,
        synthesize_status: -1
      }
    }, '', !d.loading).then(res => {
      for (let item of res) {
        item.product_money = wx.util.formatAmount(item.product_money);
      }
      res.forEach((order) => {list.push(order)});
      this.setData({
        list: list,
        loading: false,
        noMore: res.length < 10,
        pageIndex: d.pageIndex + 1
      });
    }).catch(err => {
      console.log(err);
    });
  },
  detail: function (e) {
    wx.navigateTo({
      url: `../order/detail?id=${e.currentTarget.dataset.id}`
    });
  }
})